/**
 * Created by weiwei on 11/6/18.
 */
import Tool from './tool';

/**
 * 存储类
 */
let __instance = (function () {
    let instance;
    return (newInstance) => {
        if (newInstance) instance = newInstance;
        return instance;
    }
}());

export default class Storage {

    constructor() {
        if (__instance()) return __instance();

        //init

        __instance(this);
    }

    static sharedInstance() {
        return new Storage();
    }

    static getterFor(key) {
        if (Storage.sharedInstance()['_' + key] === undefined) {
            try {
                let value = wx.getStorageSync(key);
                if (value) {
                    Storage.sharedInstance()['_' + key] = value;
                }
            } catch (e) {
                Storage.sharedInstance()['_' + key] = undefined;
            }
        }
        return Storage.sharedInstance()['_' + key];
    }

    static setterFor(key, value) {
        Storage.sharedInstance()['_' + key] = undefined;
        wx.setStorageSync(key, value);
    }

    /**
     * 微信用户信息
     * @returns {undefined|*|void}
     */
    static wxUserInfo() {
        return this.getterFor('wxUserInfo');
    }

    static setWxUserInfo(wxUserInfo) {
        this.setterFor('wxUserInfo', wxUserInfo);
    }

    static currentMember() {
        return this.getterFor('currentMember');
    }

    static setCurrentMember(currentMember) {
        this.setterFor('currentMember', currentMember);
    }

    //登陆标记
    static didLogin() {
        return this.getterFor('didLogin');
    }

    static setDidLogin(didLogin) {
        this.setterFor('didLogin', didLogin);
    }

    //当前登录用户名
    static loginUserName() {
        return this.getterFor('loginUserName');
    }

    static setLoginUserName(loginUserName) {
        this.setterFor('loginUserName', loginUserName);
    }

    //系统信息
    static sysInfo() {
        return this.getterFor('sysInfo');
    }

    static setSysInfo(sysInfo) {
        this.setterFor('sysInfo', sysInfo);
    }

    // 获取 openId
    static setWxOpenid(Openid) {
        this.setterFor('openid', Openid)
    }

    static getWxOpenid() {
        return this.getterFor('openid');
    }

    // 获取 unionid
    static setWxUnionid(unionid) {
        this.setterFor('unionid', unionid)
    }

    static getWxUnionid() {
        return this.getterFor('unionid');
    }
    // 获取 unionid
    static setWeChatCode(weChatCode) {
        this.setterFor('weChatCode', weChatCode)
    }

    static getWeChatCode() {
        return this.getterFor('weChatCode');
    }
    // 用户账号信息

    static setUserAccountInfo(info) {
        this.setterFor('userAccountInfo', info)
    }

    static getUserAccountInfo() {
        return this.getterFor('userAccountInfo');
    }

    // 存cookie

    static setUserCookie(info) {
        this.setterFor('userCookie', info)
    }

    static getUserCookie() {
        return this.getterFor('userCookie');
    }

    // 存购物车 
    static setShoppingCart(info) {
        this.setterFor('userShoppingCart', info)
    }

    static getShoppingCart() {
        return this.getterFor('userShoppingCart');
    }

    static clearShoppingCart() {
        this.setterFor('userShoppingCart', null);
    }

    // 订单确认页面地址

    static setOrderAddress(info) {
        this.setterFor('OrderAddress', info)
    }

    static getOrderAddress() {
        return this.getterFor('OrderAddress');
    }


    // 订单搜索历史
    static setSearchOrderHistory(info) {
        this.setterFor('searchOrderHistory', info)
    }

    static getSearchOrderHistory() {
        return this.getterFor('searchOrderHistory');
    }

    // 搜索订单的数据
    static setPayOrderList(info) {
        this.setterFor('payOrderList', info)
    }

    static getPayOrderList() {
        return this.getterFor('payOrderList');
    }

    // 提交订单的参数
    static setSubmitOrderList(info) {
        this.setterFor('submitOrderList', info)
    }

    static getSubmitOrderList() {
        return this.getterFor('submitOrderList');
    }

    // getToken()

    static setToken(info) {
        this.setterFor('userToken', info)
    }

    static getToken() {
        return this.getterFor('userToken');
    }

    static setPlatform(info) {
        this.setterFor('platform', info)
    }

    static getPlatform() {
        return this.getterFor('platform');
    }

    // 回跳地址
    static setBackUrl(info) {
        this.setterFor('backUrl', info)
    }
    
    // 获取 店铺id
    static setShopId(shopId) {
        this.setterFor('shopId', shopId)
    }

    static getShopId() {
        return this.getterFor('shopId');
    }
}
